package com.fruit.auth.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.fruit.auth.dao.DepartmentDao;
import com.fruit.auth.model.Department;
import com.fruit.dao.impl.BaseDaoImpl;

@Repository("departmentDao")
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao
{

	@Override
	public List<Department> findChildDepartments(Department department)
	{
		if(department==null)
			return findEntityByHql("from Department where pid is null");
		return findEntityByHql("from Department where pid=?", department.getId());
	}

	@Override
	public Long getChildrenCount(Department department)
	{
		return (Long) findUniqueResultByHql("select count(*) from Department where pid=?", department.getId());
	}

	@Override
	public void deleteDepartment(Department department)
	{
		batchEntityByHql("update from User set department=null where department.id=?", department.getId());
		deleteEntity(department);
	}

}
